import axios from 'axios'

// import store from '@/store'
// import { getToken } from '@/utils/cookie'

// create an axios instance
const requests = axios.create({
  // contentType: 'application/json;charset=UTF-8',
  // baseURL: "192.168.10.144",
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor
requests.interceptors.request.use(
  config => {
    let token = sessionStorage.getItem('Authorization')
    if (!config.headers.hasOwnProperty('Authorization') && token) {
    config.headers.Authorization = "Bearer" + ' ' + token;
    }
    return config;
  },
  error => {
    return Promise.error(error)
  }
)

// response interceptor
requests.interceptors.response.use(    
  response => {   
      // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据     
      // 否则的话抛出错误
      if (response.status === 200) {            
          return Promise.resolve(response);        
      } else {            
          return Promise.reject(response);        
      }    
})

export default requests